package com.xhis.homenurse.tools;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.preference.PreferenceManager;
import android.provider.Settings;
import androidx.core.app.ActivityCompat;
import androidx.appcompat.app.AlertDialog;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.Display;
import android.view.WindowManager;

import com.xhis.homenurse.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 张兵 on 2019/6/28.
 */
public class DeviceInfo
{
    public String PhoneNumber = "";
    public String IMEI = "";
    public String MAC = "";
    public String SN = "";
    public String SimSN = "";
    public String AndroidId = "";
    public String Type = "";
    public static final int REQUEST_PERMISSION = 998;

    public void getDeviceInfo(final Context context)
    {
        SharedPreferences defaultSp = PreferenceManager.getDefaultSharedPreferences(context);
        try {
            final List<String> permissionList = new ArrayList<>();
            if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
                permissionList.add(Manifest.permission.READ_PHONE_STATE);
            }
            if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_SMS) != PackageManager.PERMISSION_GRANTED) {
                permissionList.add(Manifest.permission.READ_SMS);
            }
            if (permissionList.size() > 0) {
                AlertDialog.Builder builder = new AlertDialog.Builder(context);
                builder.setCustomTitle(DialogTools.getDialgoTitleView(context, "提醒", R.mipmap.hintdialog));
                builder.setMessage("由于信息中心需要管理移动护士站设备，统计登录设备信息，所以请开通读取SIM卡、手机状态的权限");
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener()
                {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i)
                    {
//                        Intent intent = new Intent();
//                        if (Build.VERSION.SDK_INT >= 24) {
//                            // 适配android7.0 ，不能直接访问原路径
//                            // 需要对intent 授权
//                            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
//                            intent.setDataAndType(FileProvider.getUriForFile(this, "com.newstar.mobileoutpatientinfusion", new File(fullName)), "application/vnd.android.package-archive");
//                        } else {
//                            intent.setDataAndType(Uri.fromFile(new File(fullName)),"application/vnd.android.package-archive");
//                        }
                        ActivityCompat.requestPermissions((Activity) context, permissionList.toArray(new String[permissionList.size()]), REQUEST_PERMISSION);
                    }
                });
                builder.setCancelable(false);
                builder.show();
                return;
            }

        } catch (SecurityException e) {
            e.printStackTrace();
        }


        getPhoneNumber(context);
        getIMEI(context);
        getAndroidId(context);
        getMac(context);
        getSimSN(context);
        getSN(context);
        getType(context);
        SharedPreferences.Editor editor = defaultSp.edit();
        editor.putString("PhoneNumber", PhoneNumber);
        editor.putString("IMEI", IMEI);
        editor.putString("AndroidId", AndroidId);
        editor.putString("MAC", MAC);
        editor.putString("SimSN", SimSN);
        editor.putString("SN", SN);
        editor.putString("Type", Type);
        Log.i("DeviceInfo", toString());
    }

    public void getPhoneNumber(Context context) throws SecurityException
    {

        TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        PhoneNumber = telephonyManager.getLine1Number();
        if (PhoneNumber == null || PhoneNumber.equals("null"))
            PhoneNumber = "";


    }

    public void getIMEI(Context context) throws SecurityException
    {
        String imei;

        TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        IMEI = telephonyManager.getDeviceId();
        if (IMEI == null || IMEI.equals("null"))
            IMEI = "";
    }

    public void getMac(Context context)
    {
        //wifi mac地址
        WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        WifiInfo info = wifi.getConnectionInfo();
        MAC = info.getMacAddress();
        if (MAC == null || MAC.equals("null"))
            MAC = "";
    }

    public void getSimSN(Context context) throws SecurityException
    {
        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        SimSN = tm.getSimSerialNumber();
        if (SimSN == null || SimSN.equals("null"))
            SimSN = "";
    }

    public void getAndroidId(Context context)
    {
        AndroidId = Settings.System.getString(context.getContentResolver(), Settings.System.ANDROID_ID);
        if (AndroidId == null || AndroidId.equals("null"))
            AndroidId = "";
    }

    public void getSN(Context context)
    {
        SN = Build.SERIAL;
        if (SN == null || SN.equals("null"))
            SN = "";
    }

    @Override
    public String toString()
    {
        return "DeviceInfo{" +
                "PhoneNumber='" + PhoneNumber + '\'' +
                ", IMEI='" + IMEI + '\'' +
                ", MAC='" + MAC + '\'' +
                ", SN='" + SN + '\'' +
                ", SimSN='" + SimSN + '\'' +
                ", AndroidId='" + AndroidId + '\'' +
                '}';
    }

    public void getType(Context context)
    {
        String type = "";
        boolean ispad = (context.getResources().getConfiguration().screenLayout
                & Configuration.SCREENLAYOUT_SIZE_MASK)
                >= Configuration.SCREENLAYOUT_SIZE_LARGE;
        if (ispad)
            type = "平板";
        else {
            if (Build.DEVICE != null && Build.DEVICE.contains("MT90")) {
                type = "PDA";
            } else
                type = "手机";
        }
        Type = type;
    }

    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public static int dip2px(Context context, float dpValue)
    {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    /**
     * 根据手机的分辨率从 px(像素) 的单位 转成为 dp
     */
    public static int px2dip(Context context, float pxValue)
    {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

    //获取屏幕的宽度
    public static int getScreenWidth(Context context)
    {
        WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Display display = manager.getDefaultDisplay();
        return display.getWidth();
    }

    //获取屏幕的高度
    public static int getScreenHeight(Context context)
    {
        WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Display display = manager.getDefaultDisplay();
        return display.getHeight();
    }
}
